﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using ViCode_LeVi.Reports;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabBalanceAndOthers_PROFITANDLOSS.xaml
    /// </summary>
    public partial class VMTabBalanceAndOthers_Sheet_Statement : UserControl
    {
        public VMTabBalanceAndOthers_Sheet_Statement()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabBalanceAndOthers_Balance_Loaded);
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(VMTabBalanceAndOthers_PROFITANDLOSS_DataContextChanged);
        }

        void VMTabBalanceAndOthers_PROFITANDLOSS_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            RefreshView();
        }
        Step_BalanceAndOthers_Sheet_Statement _SHEETSTATEMENT = null;
        void VMTabBalanceAndOthers_Balance_Loaded(object sender, RoutedEventArgs e)
        {
            _SHEETSTATEMENT = this.DataContext as Step_BalanceAndOthers_Sheet_Statement;
           // RefreshView();
        }
        private void RefreshView()
        {
           
            //if (_PROFIT != null)
            //{
            //    _PROFIT.RefreshData();
            //    GenUI(_PROFIT);
            //    SetupButton_AddNew();
            //}
        }
        #region Gen
        private void GenUI(Step_BalanceAndOthers_Sheet_Statement balance)
        {
            //if (balance == null)
            //    return;
            //if (balance.Items_INCOME == null || balance.Items_INCOME.Length == 0)
            //{
            //    balance.AddNew(balance.GROUP_INCOME);
            //}
            //GenUI(Panel_INCOME, balance.Items_INCOME);

            //if (balance.Items_COSTS == null || balance.Items_COSTS.Length == 0)
            //{
            //    balance.AddNew(balance.GROUP_COSTS);
            //}
            //GenUI(Panel_COSTS, balance.Items_COSTS);

            //if (balance.Items_OPERATINGEXPENSES == null || balance.Items_OPERATINGEXPENSES.Length == 0)
            //{
            //    balance.AddNew(balance.GROUP_OPERATINGEXPENSES);
            //}
            //GenUI(Panel_OPERATINGEXPENSES, balance.Items_OPERATINGEXPENSES);           
        }
        private void GenUI(Panel panel, Step_BalanceAndOthers_Sheet_Statement_Item[] items)
        {
            //if (items == null || items.Length == 0)
            //{
            //    panel.Children.Clear();
            //}
            //else
            //{
            //    ////Collapse all
            //    //foreach(FrameworkElement child in panel.Children)
            //    //{
            //    //    child.Visibility = Visibility.Collapsed;
            //    //}
            //    //add
            //    var children = panel.Children.Cast<FrameworkElement>().ToArray();
            //    List<FrameworkElement> listInUI = new List<FrameworkElement>();
            //    foreach (var item in items)
            //    {
            //        var child = children.FirstOrDefault(d => d.DataContext == item);
            //        if (child == null)
            //        {
            //            child = new VMTabBalanceAndOthers_Sheet_Statement_Item()
            //            {
            //                DataContext = item
            //                  ,
            //                Action_Remove = (removeItem) =>
            //                {
            //                    if (_PROFIT != null)
            //                    {
            //                        if (MessageBox.Show("Do you want to remove this item?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            //                        {
            //                            _PROFIT.Remove(removeItem);
            //                            if (removeItem.Parent != null)
            //                                removeItem.Parent.SendPropertyChanged_Total();
            //                            GenUI(_PROFIT);
            //                        }
            //                    }
            //                }
            //            };
            //            panel.Children.Add(child);
            //        }
            //        listInUI.Add(child);
               // }
                //remove old
            //    List<FrameworkElement> listWillRemove = new List<FrameworkElement>();
            //    foreach (FrameworkElement child in panel.Children)
            //    {
            //        if (!listInUI.Contains(child))
            //            listWillRemove.Add(child);
            //    }
            //    foreach (var removeItem in listWillRemove)
            //    {
            //        panel.Children.Remove(removeItem);
            //    }
            //}
        }
        #endregion

        #region Button add new
        private void SetupButton_AddNew()
        {
            //Button_AddNew_INCOME.Click -= new RoutedEventHandler(Button_AddNew_INCOME_Click);
            //Button_AddNew_INCOME.Click += new RoutedEventHandler(Button_AddNew_INCOME_Click);

            //Button_AddNew_COSTS.Click -= new RoutedEventHandler(Button_AddNew_COSTS_Click);
            //Button_AddNew_COSTS.Click += new RoutedEventHandler(Button_AddNew_COSTS_Click);

            //Button_AddNew_OPERATINGEXPENSES.Click -= new RoutedEventHandler(Button_AddNew_OPERATINGEXPENSES_Click);
            //Button_AddNew_OPERATINGEXPENSES.Click += new RoutedEventHandler(Button_AddNew_OPERATINGEXPENSES_Click);
        }

        
        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Report_Balance_Sheet_Statement rep = new Report_Balance_Sheet_Statement();
            var datasource = new List<Step_BalanceAndOthers_Sheet_Statement>();
            datasource.Add(_SHEETSTATEMENT);
            frmViewReport frm = new frmViewReport(rep, datasource);
            frm.Show();
        }
    }
}
